/**
 * 	 Copyright (c) 2008, 2009 Lukas Zaruba
 * 
 *   This file is part of UniAnalyzer (Universal Analyzer).
 *
 *   UniAnalyzer is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   UniAnalyzer is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with UniAnalyzer. If not, see <http://www.gnu.org/licenses/>.
 */

package net.zarubsys.unianalyzer.dataviews.lpt.gui.graph.provider;

import java.text.DecimalFormat;

import net.zarubsys.unianalyzer.dataminers.lpt.results.IResult;

/**
 * MetadataProvider
 *
 * @author  &lt;A HREF=&quot;mailto:lukas.zaruba@gmail.com&quot;&gt;Lukas Zaruba&lt;/A&gt
 * @version $Revision$ $Date$
 */
public class MetadataProvider {
	public static final int DECIMAL = 1;
	public static final int HEX = 2;
	public static final int BINARY = 3;
	
	public static final int SECOND = 1;
	public static final int MILISECOND = 1000;
	public static final int MICROSECOND = 1000000;
	
	private final IResult result;
	private final int style;
	private final int time;
	
	private static final String[] timeUnits = new String[] {"s", "ms", "\u00B5s"};
	
	public MetadataProvider(IResult result, int style, int time) {
		this.result = result;
		this.style = style;
		this.time = time;
	}
	
	public String getSampleDesc(int sampleNumber) {
		return sampleNumber + " (" + getSampleTime(sampleNumber) + ")";
	}
	
	public String getSampleRangeDesc(int range) {
		return getSampleDesc(range);
	}
	
	public String getValueWithUnits(int position) {
		String valueString;
		int contentAtRow = result.getContentAtPosition(position);
		switch (style) {
		case DECIMAL:
			valueString = String.valueOf(contentAtRow);
			break;
		case HEX:
			valueString = Integer.toHexString(contentAtRow) + "h";
			break;
		case BINARY:
			valueString = Integer.toBinaryString(contentAtRow) + "b";
			break;
		default:
			throw new IllegalArgumentException("Illegal row number: It must be DECIMAL, HEX or BINARY");
		
		}
		return valueString;
	}
	
	private String getSampleTime(int sampleNumber) {
		float timeValue = sampleNumber / result.getRealFrequency(); // TODO check logic
		return formatValueWithUnits(timeValue);
	}

	private String formatValueWithUnits(float timeValue) {
		DecimalFormat df;
		String units;
		switch (time) {
		case 1:
			df = new DecimalFormat("0.000000");
			units = timeUnits[0];
			break;
		case 1000:
			df = new DecimalFormat("0.000");
			timeValue *= 1000;
			units = timeUnits[1];
			break;
		case 1000000:
			df = new DecimalFormat("0.0");
			timeValue *= 1000 * 1000;
			units = timeUnits[2];
			break;
		default:
			throw new IllegalArgumentException("Illegal time value: Time value has to be 1, 1000, or 1000000!!!");
		}
		return df.format(timeValue) + " " + units;
	}

}


/*
 * $Log$
 */